package com.erp.servlet;

import com.erp.bean.Commodity;
import com.erp.bean.Entrepot;
import com.erp.bean.Inventory;
import com.erp.service.CommodityService;
import com.erp.service.EntrepotService;
import com.erp.service.InventoryService;
import com.erp.service.impl.CommodityServiceImpl;
import com.erp.service.impl.EntrepotServiceImpl;
import com.erp.service.impl.InventoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author lyb
 * 创建选择商品控制器
 */
@WebServlet("/choose.action")
public class ChooseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收用户传入仓库信息
        String inputname= req.getParameter("inputname");
        String outname=req.getParameter("outname");
        //判断
        if (inputname.equals(outname)){
            //存入错误信息
            req.setAttribute("mess","转入与转出仓库不能相同请重写选择");
            EntrepotService entrepotService=new EntrepotServiceImpl();
            List<Entrepot> list=entrepotService.findAll();
            //将仓库列表放入作用域
            req.setAttribute("list",list);
            req.getRequestDispatcher("dispatchlist.jsp").forward(req,resp);

        }else {

            //类型转化
            int inputid=Integer.valueOf(inputname);
            int outid=Integer.valueOf(outname);
            //查询仓库中的商品
            req.setAttribute("outid",outid);
            req.setAttribute("inputid",inputid);
            InventoryService inventoryService=new InventoryServiceImpl();
            List<Inventory> invens=inventoryService.findByEntid(outid);
            CommodityService commodityService=new CommodityServiceImpl();
            List<Commodity> commcods= commodityService.findAll();
            req.setAttribute("invens",invens);
            req.setAttribute("commcods",commcods);
            //转发到商品选择页面
            req.getRequestDispatcher("dispacommond.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
